package com.vast.admin.api.wechatpay;

import cn.hutool.core.io.resource.ClassPathResource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.*;

@Component
@Slf4j
public class ZfwWXPayConfig extends WXPayConfig {

    @Value("${wechat.subscription.appId}")
    private String appId = "wx60df5c0505fe8568";

    @Value("${wechat.pay.mchId}")
    private String mchId = "1538604801";

    @Value("${wechat.pay.secret}")
    private String paySecret = "07e47d598baf4fad95bfc5f9e97f8438";

    @Value("${wechat.pay.certPath}")
    private String certPath = "wechat/apiclient_cert.p12";

    private byte[] certData;

    public ZfwWXPayConfig() {
        try {
            ClassPathResource classPathResource = new ClassPathResource(certPath);
            InputStream certStream = classPathResource.getStream();
            this.certData = IOUtils.toByteArray(certStream);
            certStream.read(this.certData);
            certStream.close();
        } catch (Exception e) {
            log.error("微信支付API证书加载失败", e);
        }
    }



    @Override
    String getAppID() {
        return appId;
    }

    @Override
    String getMchID() {
        return mchId;
    }

    @Override
    String getKey() {
        return paySecret;
    }

    @Override
    InputStream getCertStream() {
        return new ByteArrayInputStream(this.certData);
    }

    @Override
    IWXPayDomain getWXPayDomain() {
        return new WXPayDomain();
    }
}
